package com.sunday.common.statemachine.study.e8_History;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.configurers.StateConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;


@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States3, Events> {

    @Override
    public void configure(StateMachineConfigurationConfigurer<States3, Events> config) throws Exception {
        config
                .withConfiguration()
//                .autoStartup(true)
                .listener(listener());
    }


    @Override
    public void configure(StateMachineStateConfigurer<States3, Events> states) throws Exception {
        states
                .withStates()
                .initial(States3.S1) // 初始状态为 S1
                .state(States3.S2)    // 定义 S2 状态
                .and()

                .withStates()
                .parent(States3.S2)  // S21 和 S22 是 S2 的子状态
                .initial(States3.S2I) // 初始子状态为 S2I
                .state(States3.S21)  // 子状态 S21
                .state(States3.S22)  // 子状态 S22
                .history(States3.SH, StateConfigurer.History.SHALLOW)  // 配置历史记录为 SHALLOW
                .and();
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States3, Events> transitions) throws Exception {
        transitions
                .withExternal()
                .source(States3.S1)
                .target(States3.S2)
                .event(Events.TO_S2)
                .and()

                .withExternal()
                .source(States3.S2)
                .target(States3.S21)
                .event(Events.TO_S21)
                .and()

                .withExternal()
                .source(States3.S21)
                .target(States3.S22)
                .event(Events.TO_S22)
                .and()

//                .withExternal()
//                .source(States3.S22)
//                .target(States3.SH)
//                .event(Events.TO_SH)
//                .and()

                .withHistory()
                .source(States3.SH)  // 使用历史状态 SH
                .target(States3.S22)  // 通过历史状态切换到 S22
        ;
    }

    /**
     * <a href="https://docs.spring.io/spring-statemachine/docs/4.0.0/reference/index.html#statemachine-config-commonsettings">...</a>
     * 该接口在内部用于 对状态机的结构进行一些健全性检查。其目的是 fail fast，而不是让常见的配置错误进入 状态机。默认情况下，会自动启用验证程序并使用 implementation 。StateMachineModelVerifierDefaultStateMachineModelVerifier
     * 使用 ，如果满足以下条件，您可以禁用验证程序或设置自定义验证程序 需要。以下示例显示了如何执行此操作：withVerifier()
     */
    // 配置状态机监听器，记录状态变化
    @Bean
    public StateMachineListener<States3, Events> listener() {
        return new StateMachineListenerAdapter<>() {
            @Override
            public void stateChanged(State<States3, Events> from, State<States3, Events> to) {
                System.out.println("State changed from " + (from == null ? "null" : from.getId()) +
                        " to " + (to == null ? "null" : to.getId()));
            }
        };
    }
}
